package com.lz.sports.vm.checkmodel

import androidx.lifecycle.LifecycleOwner
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lz.helper.idcard.IDCardHelper
import com.lz.helper.idcard.IDCardInfo
import com.lz.library.base.BaseModel
import com.lz.library.base.BaseViewModel
import com.lz.library.extension.APP
import com.lz.sports.core.SoundHelper
import kotlinx.coroutines.delay

//刷身份证
class IdCardViewModel : BaseViewModel<BaseModel>(), IDCardHelper.IDCardCallback {

    //刷身份证
    private val helper by lazy { IDCardHelper(APP, true, this) }

    private val soundHelper by lazy { SoundHelper() }

    override fun onCreate(owner: LifecycleOwner) {
        soundHelper.init(intArrayOf(com.lz.helper.R.raw.idcard_read, com.lz.helper.R.raw.idcard_success))
        helper.start()
    }

    override fun onResume(owner: LifecycleOwner) {
        launch {
            delay(500)
            soundHelper.play(0)
        }
        helper.restart()
    }

    override fun onPause(owner: LifecycleOwner) {
        helper.pause()
    }

    override fun onCleared() {
        super.onCleared()
        helper.quit()
        soundHelper.release()
    }

    override fun onCardResult(idCardInfo: IDCardInfo) {
        soundHelper.play(1)
        LiveEventBus.get("idCardInfo", IDCardInfo::class.java).post(idCardInfo)
    }

    override fun onCardError(err: Int) {
    }
}